<?php
require_once APPPATH.'models/Generic_Dataset_Model.php';

class Usuarios_model extends Generic_Dataset_Model
{

    function __construct()
    {
        parent :: __construct();
        $this->table = 'usuarios';
        $this->primary_key = 'usu_id';
    }

    function checklogin_in_DB($nick, $clave)
    {
        $nick = strtoupper($nick);
        $clave = strtoupper($clave);
        $this->db->select('usu_id, usu_login, usu_nombre');
        $this->db->select('UPPER(usu_password) as usu_password');
        $this->db->where('UPPER(usu_login)', $nick);
        $this->db->limit(1);
        $query = $this->db->get('usuarios');
        if ($query->num_rows() == 0)
        {
            $data['resultado'] = 'error';
            $data['mensaje'] = 'El usuario no se encuentra registrado';
        } else
        {
            $row = $query->row();
            if ($row->usu_password != $clave)
            {
                $data['resultado'] = 'error';
                $data['mensaje'] = 'La contraseña no corresponde con el usuario';
            } else
            {
                $data['resultado'] = 'ok';
                $this->session->set_userdata('id_usuario', $row->usu_id);
            }
        }
        return $data;
    }

    function griddeusuario()
    {
        $this->load->library('datagrid');
        $grid = $this->datagrid;
        /* $grid->addField('usu_id');
         $grid->label('usu_id');
         $grid->params(array('align' => "'center'", 'width' => 50, 'editable' => 'false', 'editoptions' => '{readonly:true,size:10}'));*/
        $grid->addField('usu_login');
        $grid->label('Login');
        $grid->params(array('width' => 300, 'editable' => 'true', 'edittype' => "'text'", 'editrules' => '{required:true}'));
        $grid->addField('usu_nombre');
        $grid->label('Nombre');
        $grid->params(array('width' => 350, 'editable' => 'true', 'edittype' => "'text'", 'editrules' => '{required:true}'));
        $grid->addField('usu_password');
        $grid->label('Contraseña');
        $grid->params(array('width' => 300, 'editable' => 'true', 'edittype' => "'text'", 'editrules' => '{required:true}'));
        $grid->showpager(true);
        $grid->setWidth('600');
        $grid->setHeight('200');
        $grid->setTitle('Usuarios');
        #Botones de navegación
        $grid->setAdd(true);
        $grid->setEdit(true);
        $grid->setDelete(true);
        $grid->setSearch(true);
        $grid->setRowNum(20);
        #GET url
        $grid->setUrlget(site_url('usuarios/getdata/'));
        #Set url
        $grid->setUrlput(site_url('usuarios/setdata/'));
        return $grid->deploy();
    }
}
